package com.ce.wnd;

import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import net.sf.json.JSONObject;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.PostMethod;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.ProgressBar;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.swt.widgets.TreeItem;

import com.ce.po.VChannelDO;
import com.ce.thd.InitTreeThread;
import com.ce.thd.UpdateProgressRunnable;
import com.ce.thd.UpdateProgressThread;
import com.google.gson.Gson;

public class TestWnd extends Composite {
	private Tree tree_1;
	private Tree tree;
	private Combo combo;
	private static String weburl = "http://etv.easy3c.net/tags.php";
	private List<VChannelDO> channels = new ArrayList<VChannelDO>();
	
    private static void checkParentsNode(TreeItem item, boolean state)  
    {  
        item.setChecked(state);  
        TreeItem parent = item.getParentItem();  
        if(parent != null) {  
            parent.setChecked(state);  
            checkParentsNode(parent, state);  
        }  
    } 

    private void checkChildrenNode(TreeItem item, boolean state)  
    {  
        item.setChecked(state);  
        TreeItem[] items = item.getItems();  
        for (TreeItem treeItem : items)  
        {  
            checkChildrenNode(treeItem, state);  
        }  
    } 
    
	public TestWnd(Composite parent, int style) {
		super(parent, style);

		final Button testhttpButton = new Button(this, SWT.NONE);
		testhttpButton.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(final SelectionEvent e) {
				try {
					HttpClient httpClient = new HttpClient();
					PostMethod postMethod = new PostMethod(weburl);
					postMethod.addRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=utf-8");
					String responseMsg = "";
					String channel = (String)combo.getData(combo.getItem(combo.getSelectionIndex()));
					System.out.println(channel);
					postMethod.setParameter("channel", channel);
					int statusCode = httpClient.executeMethod(postMethod);
					String statusText = postMethod.getStatusText();
					responseMsg = postMethod.getResponseBodyAsString();
					//responseMsg = URLDecoder.decode(responseMsg, "utf-8");
					System.out.println(responseMsg);
					
					JSONObject object = JSONObject.fromObject(responseMsg);
					System.out.println(object);
				} catch (Exception e1) {
					e1.printStackTrace();
				}
				
			}
		});
		testhttpButton.setText("Http");
		testhttpButton.setBounds(10, 10, 48, 22);

		final Button jsonButton = new Button(this, SWT.NONE);
		jsonButton.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(final SelectionEvent selectionevent) {
				Gson gson = new Gson();
				List<String> list = new ArrayList<String>();
				list.add("test");
				System.out.println(gson.toJson(list));
			}
		});
		jsonButton.setText("Json");
		jsonButton.setBounds(64, 10, 48, 22);

		combo = new Combo(this, SWT.NONE);
		combo.setItems(new String[] {"电视剧", "电影", "综艺", "儿童", "音乐", "动漫"});
		combo.setData("电视剧", "1");
		combo.setData("电影", "2");
		combo.setData("综艺", "3");
		combo.setData("儿童", "4");
		combo.setData("音乐", "5");
		combo.setData("动漫", "6");
		combo.setVisibleItemCount(8);
		combo.select(0);
		combo.setBounds(10, 51, 106, 20);

		final Button pathButton = new Button(this, SWT.NONE);
		pathButton.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(final SelectionEvent e) {
				try {
					URL url = TestWnd.class.getClassLoader().getResource("/");
					System.out.println(url.getPath());
				} catch (Exception e1) {
					e1.printStackTrace();
				}
			}
		});
		pathButton.setText("Path");
		pathButton.setBounds(118, 10, 48, 22);

		final ProgressBar progressBar = new ProgressBar(this, SWT.NONE);
		progressBar.setBounds(122, 51, 289, 18);
		progressBar.setMinimum(0);
		progressBar.setMaximum(100);

		tree = new Tree(this, SWT.BORDER | SWT.CHECK);
		tree.setBounds(388, 81, 100, 175);
		tree.addListener(SWT.Selection, new Listener()  
        {  
            public void handleEvent(Event event)  
            {  
                if (event.detail == SWT.CHECK)  
                {  
                    TreeItem item = (TreeItem) event.item;  
                    boolean state = item.getChecked();  
                    checkChildrenNode(item, state);  
                    checkParentsNode(item, state);  
                }  
            }  
        });

		final Button syncButton = new Button(this, SWT.NONE);
		syncButton.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(final SelectionEvent e) {
//				Thread up = new Thread(new UpdateProgressRunnable(progressBar));
//				up.start();
				Display.getDefault().syncExec(new UpdateProgressRunnable(progressBar));
			}
		});
		syncButton.setText("sync");
		syncButton.setBounds(172, 10, 48, 22);

		final Button asyncButton = new Button(this, SWT.NONE);
		asyncButton.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(final SelectionEvent e) {
				Display.getDefault().asyncExec(new UpdateProgressRunnable(progressBar));
			}
		});
		asyncButton.setText("async");
		asyncButton.setBounds(226, 10, 48, 22);

		final Button threadButton = new Button(this, SWT.NONE);
		threadButton.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(final SelectionEvent e) {
				UpdateProgressThread th = new UpdateProgressThread(Display.getDefault(), progressBar);
				th.start();
			}
		});
		threadButton.setText("thread");
		threadButton.setBounds(280, 10, 48, 22);

		final Button treeButton = new Button(this, SWT.NONE);
		treeButton.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(final SelectionEvent e) {
				for (int i = 0; i < 5; i++) {
					TreeItem rootitem = new TreeItem(tree,SWT.NONE);
					rootitem.setText("root"+i);
					for (int j = 0; j < 3; j++) {
						TreeItem item = new TreeItem(rootitem,SWT.NONE);
						item.setText("item"+i);
						for (int k = 0; k < 4; k++) {
							TreeItem citem = new TreeItem(item,SWT.NONE);
							citem.setText("citem"+i);
						}
					}
				}
			}
		});
		treeButton.setText("tree");
		treeButton.setBounds(334, 10, 48, 22);
		
		final Button treeselButton = new Button(this, SWT.NONE);
		treeselButton.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(final SelectionEvent e) {
				TreeItem[] items = tree.getItems();
				for (int i = 0; i < items.length; i++) {
					System.out.println(items[i].getText()+"="+items[i].getChecked());
					travelTree(items[i]);
				}
			}
		});
		treeselButton.setText("treesel");
		treeselButton.setBounds(388, 10, 48, 22);
		
		tree_1 = new Tree(this, SWT.BORDER | SWT.CHECK);
		tree_1.setBounds(12, 105, 187, 225);
		
		final ProgressBar progressBar1 = new ProgressBar(this, SWT.NONE);
		progressBar1.setBounds(10, 347, 478, 18);

		final Button initButton = new Button(this, SWT.NONE);
		initButton.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(final SelectionEvent e) {
				initTree();
			}
		});
		initButton.setText("init");
		initButton.setBounds(10, 77, 48, 22);
	}
	
	private void initTree() {
		channels.add(new VChannelDO("1", "电视剧"));
		channels.add(new VChannelDO("2", "电影"));
		channels.add(new VChannelDO("3", "综艺"));
		channels.add(new VChannelDO("4", "儿童"));
		channels.add(new VChannelDO("5", "音乐"));
		channels.add(new VChannelDO("6", "动漫"));
		
/*		InitTreeThread initth = new InitTreeThread(channels,tree_1,Display.getDefault());
		initth.start();*/
	}
	private void travelTree(TreeItem node) {
		if(node.getItemCount() == 0)
			return;
		TreeItem[] items = node.getItems();
		for (int i = 0; i < items.length; i++) {
			System.out.println(items[i].getText()+"="+items[i].getChecked());
			travelTree(items[i]);
		}
	}

}
